package com.helloUIBinder.client.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.helloUIBinder.client.events.OnPageClickEvent;

public class PageLinkWidget extends Composite {
    private static PageLinkWidgetUiBinder uiBinder = GWT.create(PageLinkWidgetUiBinder.class);

    @UiField
    Anchor linkName;

    private SimpleEventBus eventBus;

    interface PageLinkWidgetUiBinder extends UiBinder<Widget, PageLinkWidget> {
    }


    public PageLinkWidget(SimpleEventBus eventBus) {
        initWidget(uiBinder.createAndBindUi(this));
        this.eventBus = eventBus;
        linkName.setStyleName("page-link");
    }

    public PageLinkWidget(SimpleEventBus eventBus, Boolean current) {
        initWidget(uiBinder.createAndBindUi(this));
        this.eventBus = eventBus;
        if (current) {
            linkName.setStyleName("page-link-choosed");
        }else{
            linkName.setStyleName("page-link");
        }
    }

    public void setLinkName(String name) {
        linkName.setText(name);
    }

    @UiHandler("linkName")
    void onLinkClick(ClickEvent event) {
        OnPageClickEvent onPageClickEvent = new OnPageClickEvent();
        onPageClickEvent.setClickedLinkName(event.getRelativeElement().getInnerText());
        eventBus.fireEvent(onPageClickEvent);
    }
}
